import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {resolve} from 'path'
import createVitePlugins from './vite/plugins'
// let target = 'http://192.168.10.63:8080'
let target = 'http://47.98.179.188:7124'

//vite文档: https://vitejs.dev/config/
export default defineConfig(({mode, command}) => {
    //comman用于判断生产、开发模式：https://cn.vitejs.dev/config/#conditional-config
    return {
        base: '/vue3/',//evaluate
        plugins: [
            createVitePlugins(command)
        ],
        resolve: {
            // https://cn.vitejs.dev/config/#resolve-alias
            alias: {// 设置别名
                '@': resolve(__dirname, './src')
            },
            // https://cn.vitejs.dev/config/#resolve-extensions
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
        },
        css: {
            preprocessorOptions: {
                /*全局css变量*/
                less: {
                    javascriptEnabled: true,
                    additionalData: '@import "./src/assets/styles/variables.module.less";',
                },
            },
        },
        hmr: true,//热更新
        //启动端口、代理
        server: {
            port: 3333,
            host: true,
            open: false,
            // hmr: true,//热更新
            proxy: {
                '/v1': {
                    target: target,
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/v1/, '/v1')
                }
            }
        },
        // 打包配置
        build: {
            outDir: 'dist',
            chunkSizeWarningLimit: 1500,
            rollupOptions: {
                output: {
                    chunkFileNames: 'assets/js/[name]-[hash].js',
                    entryFileNames: 'assets/js/[name]-[hash].js',
                    assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups.moduleName ?? 'vender';
                        }
                    },
                },
            },
        }
    }
})
